package cc.sheji.controller;

import cc.sheji.Service.UserService;
import cc.sheji.domain.*;
import cc.sheji.exception.NotUserException;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Random;

@Validated
@RestController
@RequestMapping(value = "/api/v1/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("login")
    public JsonResult login(@RequestBody @Validated(User.AddeGroup.class) User user) throws Exception {

        return JsonResult.Success(userService.login(user.getName(), user.x()));
    }
    @RequestMapping("readMeassage")
    public JsonResult readMeassage() throws Exception {

        return JsonResult.Success(userService.readMeassage());
    }

    @RequestMapping("login/phone")
    public JsonResult loginPhone(@RequestBody @Validated(User.codeGroup.class) User user) throws Exception {

        return JsonResult.Success(userService.loginPhone(user.getPhone(), user.getCode()));
    }

    @RequestMapping("addCategory")
    public JsonResult addCategory(@RequestBody ArrayList<Long> ids) throws Exception {

        return JsonResult.Success(userService.addCategory(ids));
    }

    @GetMapping("/openId")
    public JsonResult openId(String code)  {
        return JsonResult.Success(userService.getWXOpenId(code));
    }


    @GetMapping("/messageInfo")
    public JsonResult messageInfo()  {
        return JsonResult.Success(userService.getMessageInfo());
    }

    @GetMapping("/focuslist")
    public JsonResult focusList(UserFocusQueryCriteria criteria, Pageable pageable) throws InterruptedException {
        return JsonResult.Success(userService.focusList(criteria, pageable));
    }

    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable Long id) {

        return JsonResult.Success(userService.get(id));
    }

    @RequestMapping("/mine")
    public JsonResult get() {

        return JsonResult.Success(userService.get());
    }

    @PostMapping("register")
    public JsonResult create(@RequestBody @Validated(User.AddeGroup.class) User user) {

        user.setId(null);

        int num = new Random().nextInt(99) + 1;
        user.setAvator("https://www.shejiai.cc/v1/image/head"+num+".png");

        try {
            return JsonResult.Success(userService.create(user));
        } catch (DataIntegrityViolationException e) {
            return new JsonResult(ErrorCode.ExistUserException, "用户名已存在");
        }
    }

    @PostMapping("focus")
    public JsonResult focus(@RequestBody @Validated(User.AddeGroup.class) UserFocus entity) {
        return JsonResult.Success(userService.focus(entity));
    }

    @PostMapping("sign")
    public JsonResult sign() {
        return JsonResult.Success(userService.sign());
    }

    @DeleteMapping("deleteFocus")
    public JsonResult deleteFocus(@RequestBody @Validated(User.AddeGroup.class) UserFocus entity) {
        return JsonResult.Success(userService.deleteFocus(entity));
    }

    @PutMapping()
    public JsonResult update( @Valid @RequestBody User user) {

        user.setId(StpUtil.getLoginIdAsLong());

        return JsonResult.Success(userService.update1(user));
    }

    @PutMapping("/updatePassword")
    public JsonResult updatePassword(@RequestBody User user) throws NotUserException {

        return JsonResult.Success(userService.updatePassword(user));
    }

    @GetMapping("")
    public JsonResult list(UserQueryCriteria criteria, Pageable pageable) {
        return JsonResult.Success(userService.list(criteria, pageable));
    }

    /**
     * 邀请注册
     * @param user
     * @return
     */
    @PostMapping("invitetion")
    public JsonResult invitetion(@RequestBody @Validated(User.AddeGroup.class) User user){

        user.setId(null);

        int num = new Random().nextInt(99) + 1;
        user.setAvator("https://www.shejiai.cc/v1/image/head"+num+".png");

        try {
            return JsonResult.Success(userService.invitationUser(user));
        } catch (DataIntegrityViolationException e) {
            return new JsonResult(ErrorCode.ExistUserException, "用户名已存在");
        }
    }

}
